const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    items: [],
    pdVisible: false,
    pdText: '',
    loadingVisible: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  getData() {
    app.ajax({
      data: {
        type: '41',
        page: this.data.page
      }
    }).then(res => {
      if (res.ret_code == 0) {
        this.setData({
          items: res.pagebean.contentlist,
          loadingVisible: false
        })
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var items = this.data.items;
    app.ajax({
      data: {
        type: '41',
        page: 1
      }
    }).then(res => {
      if (res.ret_code == 0) {
        var lists = res.pagebean.contentlist
        var newArr = app.$util.getArrDifference(items, lists);
        if (newArr.length > 0) {
          this.setData({
            pdText: newArr.length + '条新内容'
          })
        } else {
          this.setData({
            pdText: '暂无新内容'
          })
        }
        this.setData({
          pdVisible: true,
          items: newArr.concat(items)
        })
        setTimeout(() => {
          this.setData({
            pdVisible: false,
            pdText: ''
          })
        }, 1000)
        wx.stopPullDownRefresh();
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    var items = this.data.items
    this.setData({
      loadingVisible: true
    })
    var page = this.data.page + 1;
    app.ajax({
      data: {
        type: '41',
        page: page
      }
    }).then(res => {
      if (res.ret_code == 0) {
        var lists = res.pagebean.contentlist
        this.setData({
          items: items.concat(lists),
          loadingVisible: false
        })
        if (lists.length > 0) {
          this.setData({
            page: page
          })
        }
      }
    });
  },
  openWebView(e) {
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: `/pages/webview/webview?weixin_url=${url}`
    });
  }

})